package com.neepu.controller;


import com.neepu.pojo.*;
import com.neepu.service.*;
import com.neepu.util.annotation.ArchivesLog;
import com.neepu.util.common.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class EvaluationManagerController {

    @Autowired
    private InfoManagerService infoManagerService;

    @Autowired
    private IndexManagerService indexManagerService;

    @Autowired
    private ResultService resultService;

    @Autowired
    private DutresultService dutresultService;

    @Autowired
    private InvestigationService investigationService;

    // 课程评价
    @ArchivesLog(operteContent = "查看课程评价")
    @RequestMapping(value = "/evaluate/clazz")
    public String clazz(HttpServletRequest request, Model model, Integer userType){
        //获取session
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(Constants.USER_SESSION);
        if (userType==1){ // 教师  通过教师的 职工号 查询老师所教课程
            List<Scourse> list = infoManagerService.getTeacherByTeacherId(user.getUserID());
            model.addAttribute("list",list);
        }else if (userType==2) { // 学生  用学号去查询学生所选的课程
            List<Scourse> list = infoManagerService.getTeacherByStudentId(user.getUserID());
            model.addAttribute("list",list);
        }else {  // 管理员  查询所有课程的结果
            List<Scourse> list = infoManagerService.getAllTeacher();
            model.addAttribute("list",list);
        }
        return "evaluationmanagement/clazz";
    }

//   去评价  toevaluation
    @RequestMapping(value = "/toevaluation",method = RequestMethod.GET)
    public String toevaluation(Integer id,Model model){
        // 通过 id 去查询 教师和课程的信息
        Scourse scourse = infoManagerService.getTeacherById(id);
        List<TwoTarget> twolist = indexManagerService.getAllTwoTarget();
        List<TwoTarget> list = getTwoTargets(twolist);
        model.addAttribute("list",list);
        model.addAttribute("scourse",scourse);
         return "evaluationmanagement/evapage";
    }

    //培养方案评价
    @ArchivesLog(operteContent = "查看培养方案评价")
    @RequestMapping(value = "/evaluate/programme")
    public String programme(HttpServletRequest request, Model model, Integer userType){
        //获取session

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(Constants.USER_SESSION);
        if (userType == 2) {
            List<Dutresult> list = dutresultService.getProgrammeByStudentId(user.getUserID());
            model.addAttribute("list",list);
        } else {
            List<Scourse> list = dutresultService.getAllProgramme();
            model.addAttribute("list",list);
        }
        return "evaluationmanagement/programme";
    }


    // 去评价培养方案 toprogramme
    @RequestMapping(value = "/toprogramme")
    public String toprogramme(Model model,Integer id){

        model.addAttribute("id",id);
        return "evaluationmanagement/toprogramme";
    }

    //programme/result
    @RequestMapping(value = "/programme/result",method = RequestMethod.POST)
    public String toprogrammere(Model model,Integer star,String second,String three1,String three2,
                                String three3,String three4,String three5,String four,String five,
                                String six,String seven1,String seven2,String seven3,String seven4,
                                String seven5,String id4,String id6,HttpServletRequest request,Integer investigationID){

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(Constants.USER_SESSION);
        Investigation investigation = new Investigation();
        investigation.setStar(star);
        investigation.setSecond(second);
        investigation.setThree1(three1);
        investigation.setThree2(three2);
        investigation.setThree3(three3);
        investigation.setThree4(three4);
        investigation.setThree5(three5);
        investigation.setFour(four);
        investigation.setFive(five);
        investigation.setSix(six);
        investigation.setSeven1(seven1);
        investigation.setSeven2(seven2);
        investigation.setSeven3(seven3);
        investigation.setSeven4(seven4);
        investigation.setSeven5(seven5);
        investigation.setId4(id4);
        investigation.setId6(id6);
        investigation.setUserID(user.getUserID());
        investigation.setInvestigationID(investigationID);
        investigationService.insertInvestigation(investigation);
        // 更新评价状态
        dutresultService.updateDutresult(user.getUserID());
        return "evaluationmanagement/programme";
    }


    private List<TwoTarget> getTwoTargets(List<TwoTarget> twolist) {
        return getTwoTarget(twolist, indexManagerService);
    }

    static List<TwoTarget> getTwoTarget(List<TwoTarget> twolist, IndexManagerService indexManagerService) {
        List<TwoTarget> list = new ArrayList<>();
        for (TwoTarget twoTarget : twolist) {
            Target target = indexManagerService.findTargetById(twoTarget.getOneId());
            twoTarget.setTarget(target);
            list.add(twoTarget);
        }
        return list;
    }

    @RequestMapping(value = "/result",method = RequestMethod.POST)
    @ResponseBody
    public String result(Double[] array,String studentID,String teacherID,String sourceID){
        // 对应的是从1-n的二级指标的值
        //studentID，teacherID，sourceID，targetID，score
        int targetID = 1;
        for (Double score : array) {
            Result result = new Result();
            result.setScore(score);
            result.setSourceID(sourceID);
            result.setStudentID(studentID);
            result.setTeacherID(teacherID);
            result.setTargetID(targetID);
            resultService.insertResult(result);
            targetID++;
        }
        // 对该同学的该课程进行更新状态  学生id和课程id确定
        infoManagerService.updateScourse(studentID,sourceID);
        return "1";
    }
}
